//============================================================================
// Name        : AnnotatedFieldVisitor.java
// Author      : iron.yin@gmail.com
// Version     : v1.0
// Copyright   : RisingSun CO,.LTD
// Description : AnnotatedFieldVisitor.java in Java
//============================================================================
package cc.risingsun.wing.core;

import java.lang.annotation.Annotation;
import java.util.Map;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Opcodes;

import cc.risingsun.wing.core.reflect.ReflectionHelper;



/**
 *
 * @author iron.yin@gmail.com
 * @sina   2012-8-6
 */
public class AnnotatedFieldVisitor extends FieldVisitor implements Opcodes{

	private WingField field;
	final Map<String,Class<? extends Annotation>> annotations;
	
	public AnnotatedFieldVisitor(Class<? extends Annotation>... annotations) {
		this(null,annotations);
	}
	public AnnotatedFieldVisitor(FieldVisitor fv,Class<? extends Annotation>... annotations) {
		super(ASM4, fv);
		this.annotations = ReflectionHelper.getAnnotations(annotations);
	}
	
	void setASMField(WingField field){
		this.field=field;
	}

	@Override
	public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
		if(annotations!=null&&!annotations.containsKey(desc)){
			return null;
		}
		return new AnnotationVisitorHolder(field.createAnnotation(field.getName(),desc,annotations.get(desc)),  super.visitAnnotation(desc, visible));
	}

}
